<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<s:head/>
<html>
  <head>
     <SCRIPT type="text/javascript">
        //保存费用
        function saveFee(){
           var errorMsg = "";
           var name = $("#name").val();
           var name_en = $("#name_en").val();
           
           if(name == ''){
             errorMsg += "名称,";
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveCompanySingleInfo.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           }); 
        }
        
        //保存委托
        function saveItem(){
           var errorMsg = "";
           var name = $("#name").val();
           var name_en = $("#name_en").val();
           var id = $("#id").val();
           
           if(name == ''){
             errorMsg += "名称,";
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveCompanySingleInfo.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           });
          
        }
        
         function getCboxVal(parentId){
            var right = document.getElementById("feeRight"); 
			    for(i=0; i < right.length; i++){
			    right[i].selected = true;
			}
    
            var ary = [];
            $("#feeRight").find("option").each(function(){
                 var id = $(this).val();
                 ary.push(id);
            });
		    
		    var txt = '';
		    if(ary.length>0){
		       txt = "," + ary.join(",") + ",";
		    }
		    
		    return txt;

         }
        
        // 标签
        function saveTag(){
           var name = $("#name").val();
           var errorMsg = '';
           if(name == ''){
             errorMsg += "名称,";
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveCompanySingleInfo.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           });
           
        }
        
        // 数据字典
        function saveData(){
           var name = $("#name").val();
           var name_en = $("#name_en").val();
           
           var errorMsg = '';
           if(name == ''){
             errorMsg += "名称,";
           }
           
           if(errorMsg!=''){
             errorMsg = errorMsg.replace(/,$/ , '');
             alert(errorMsg + " 不能为空");
             return;
           }
           
           var param = $("#from").serialize();
           $.post("cts/saveDataDicationary.action", param , function(data){
               if(data == 1){
                 alert("保存成功");
                 window.location.reload();
               }else if(data == 2){
                 alert("更新成功");
                 window.location.reload();
               }else{
                 alert("保存失败");
               }
           });
           
        }
        
        var type = '<s:property value="#request.type"/>';
        $(function(){
           // 委托项目
           if(type == 'item'){
            var itemSelected = '<s:property value="item.type"/>';
            $("#itemtype").find("option[value="+itemSelected+"]").attr("selected" , "selected");
           }
        });
     </SCRIPT>
     
     <style type="text/css">
       #feelist label{font-size:12px;}
       .txtright{
          text-align: right;
       }
       
     </style>
     
  </head>
  
  <body>
  
     <s:if test="#request.type == 'fee'">
	      <form action="saveCompayInfo" id="from" method="post">
	         <input type="hidden" id="type" name="type" value="<s:property value="#request.type"/>"/> 
	         <input type="hidden" id="id" name="id" value="<s:property value="#request.id"/>"/> 
	         
	         <table style="font-size:12px;" cellpadding="0" cellspacing="0" align="center">
	            <tr>
	               <td>名称：</td>
	               <td class="twidth">
	                  <input class="inputMargin" type="text" id="name" name="fee.name_zh" value="<s:property value="fee.name_zh"/>">
	                  <input type="hidden" id="name_en" name="fee.name_en"/>
	               </td>
	            </tr>
	            <tr>
	               <td>作业名称：</td>
	               <td class="twidth">
	                  <input class="inputMargin" type="text" id="itemName" name="itemName" value="<s:property value="fee.item.name"/>" onkeyup="searchUser(this , event)">
	                  <input type="hidden" id="itemId" name="itemId" value="<s:property value="fee.item.id"/>" />
	               </td>
	            </tr>
	            <tr>
	              <td>是否启用：</td>
	              <td class="twidth">
	                <input value="0" type="radio" id="status1" name="fee.status" <s:if test="fee==null || fee.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="fee.status" <s:if test="fee.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center">
	                  <input type="button" id="enterSave" value="确定" onclick="saveFee()"/>
	                  &nbsp;&nbsp;
	                  &nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	       </form>
     </s:if>
     <s:elseif test="#request.type == 'item'">
         <form action="saveCompayInfo" id="from">
	         <input type="hidden" id="type" name="type" value="<s:property value="#request.type"/>"/> 
	         <input type="hidden" id="id" name="id" value="<s:property value="#request.id"/>"/> 
	         
	         <table style="font-size:12px;width:290px;" cellpadding="0" cellspacing="0" border="0" align="center">
	            <tr>
	               <td class="txtright">名称：</td>
	               <td class="twidth">
	                 <input type="text" id="name" name="item.name_zh" value="<s:property value="item.name_zh"/>">
	                 <input type="hidden" id="name_en" name="item.name_en"/>
	               </td>
	            </tr>
	            <tr>
	              <td class="txtright">状态：</td>
	              <td class="twidth">
	                <input value="0" type="radio" id="status1" name="item.status" <s:if test="item==null || item.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="item.status" <s:if test="item.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center">
	                  <input type="button" id="enterSave" value="确定" onclick="saveItem()"/>
	                  &nbsp;&nbsp;
	                  &nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	       </form>
     </s:elseif>
     <s:elseif test="#request.type == 'tag'">
        <form action="saveCompayInfo" id="from">
	         <input type="hidden" id="type" name="type" value="<s:property value="#request.type"/>"/> 
	         <input type="hidden" id="id" name="id" value="<s:property value="#request.id"/>"/> 
	         
	         <table style="font-size:12px;" cellpadding="0" cellspacing="0" align="center">
	            <tr>
	               <td>名称：</td>
	               <td><input type="text" id="name" name="tag.name" value="<s:property value="tag.name"/>"></td>
	            </tr>
	            <tr>
	              <td>是否启用：</td>
	              <td class="twidth">
	                <input value="0" type="radio" id="status1" name="tag.status" <s:if test="tag==null || tag.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="tag.status" <s:if test="tag.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center" style="padding-top:10px;">
	                  <input type="button" id="enterSave" value="确定" onclick="saveTag()"/>
	                  &nbsp;&nbsp;
	                  &nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	       </form>
     </s:elseif>
     <s:elseif test="#request.data == 1">
         <form action="saveDataDicationary" id="from">
	         <input type="hidden" id="type" name="dataDictionary.type" value="<s:property value="#request.type"/>"/> 
	         <input type="hidden" id="typeDetail" name="dataDictionary.typeDetail" value="<s:property value="#request.typeDetail"/>"/> 
	         <input type="hidden" id="id" name="dataDictionary.id" value="<s:property value="#request.id"/>"/> 
	         
	         <table style="font-size:12px;" cellpadding="0" cellspacing="0" align="center">
	            <tr>
	               <td>名称：</td>
	               <td class="twidth"><input type="text" id="name" name="dataDictionary.name" value="<s:property value="dataDictionary.name"/>"></td>
	            </tr>
	            <tr>
	              <td>是否启用：</td>
	              <td class="twidth">
	                <input value="0" type="radio" id="status1" name="dataDictionary.status" <s:if test="dataDictionary==null || dataDictionary.status==0 ">checked</s:if>/>正常
	                <input value="1" type="radio" id="status2" name="dataDictionary.status" <s:if test="dataDictionary.status==1">checked</s:if>/>禁用
	              </td>
	            </tr>
	            <tr>
	               <td colspan="2" align="center">
	                  <input type="button" id="enterSave" value="确定" onclick="saveData()"/>
	                  &nbsp;&nbsp;
	                  &nbsp;&nbsp;
	                  <input type="reset" id="reset" value="重置"/>
	               </td>
	            </tr>
	         </table>
	     </form>
     </s:elseif>
    
  </body>
</html>

<script type="text/javascript">
  
  function searchUser(obj , e){
      if(bindNextPrev(obj , e)){
         return;
      }
      
      var name = $(obj).val();
      if(name.replace(/\s/g , '') == ""){
         $("#userId").val('');
         $("#userlist").hide();
         return;
      }
      
      var param = $("#from").serialize();
      var url = "cts/findItemToJson";
      $.post(url , param , function(data){
            var data2 = eval("("+data+")");
            showUserList(data2 , obj);
      });
   }
  
  function showUserList(data , obj){
      if(data.length <= 0){
         $("#userId").val('');
         $("#userlist").hide();
         return;
      }
      
      var mObj = $("#from").find("div[id='userlist']");
      if(mObj == null || $(mObj).html() == null ){
        var divObj = '<div id="userlist" class="userlist"><ul style="width:100%;"></ul></div>';
        $(obj).before(divObj);
      }
            
      var li="";
      for(var i=0;i<data.length;i++){
         li += '<li id='+data[i]["id"]+'>'+data[i]["name"]+'</li>';
      }
            
      $("#userlist").find("ul").html(li);
      $("#userlist").find("li").each(function(){
          $(this).click(function(){
              selectUser(this);
              $("#userlist").hide();
          }).mouseover(function(){
              $(this).css({"background-color":"#ececec"});
          }).mouseout(function(){
              $(this).css({"background-color":"#ffffff"});
          });
       });
            
       var top = $(obj).offset().top+10;
       var left = $(obj).offset().left;
       
       top = 25;
       left = 0;
       $("#userlist").css({
          "margin-top":top+"px",
          "margin-left":left+"px"
       }).show();
   }
         
  function selectUser(obj){
       var id = $(obj).attr("id");
       var name = $(obj).text();
       $("#itemId").val(id);
       $("#itemName").val(name);
   }
    
         
   function myOnblur(obj , hideId){
      var time = null;
      time = window.setTimeout(function(){
          window.clearTimeout(time);
          $("#"+hideId).hide();
      } , 500);
   }       
</script>

<script type="text/javascript">
	var selectObject = null;
	function mouseMoveCha(){
		    var contentlist = $("#userlist");
		    contentlist.each(function(i){
		        $(this).mouseout(function(){
		            //selectObject = this;
		    		$(contentlist).find("li").css({"background-color":"#fff"});
		            $(this).css({"background-color":"#ddd"});
		        }).mouseover(function(){
		        
		        });
		    });
			      
	}

    function bindNextPrev(obj , e){
          mouseMoveCha();
          
          var theEvent = window.event || e;
       if(typeof(theEvent) == 'undefined'){
         return true;
     }
       
    var keyFee = theEvent.keyCode || theEvent.which;
    var isGo = false;
	var contentlist = $("#userlist");
    
    if (keyFee == 38) {// prev
        if(selectObject == null){
           selectObject = $(contentlist).find("li:last");
        }else{
           selectObject = $(selectObject).prev();
           if($(selectObject).html() == null){
              selectObject = $(contentlist).find("li:last");
           }
        }
	    isGo = true;
	}
	
	if (keyFee == 40) {// next
	    if(selectObject == null){
           selectObject = $(contentlist).find("li:first");
        }else{
           selectObject = $(selectObject).next();
           if($(selectObject).html() == null){
              selectObject = $(contentlist).find("li:first");
           }
        }
	    isGo = true;
	}
	
	if(keyFee == 13){ // enter
	    if(selectObject!=null && $(selectObject).html() !=null){
	        selectUser(selectObject);
	    }
	    
	    $(contentlist).hide();
	    return true;
	}
	
	if(keyFee == 27){
	   $(contentlist).hide();
	    return true;
	}
	
	// change the background color
	if(isGo == true){
	   if($(selectObject).html() !=null){
	      $(contentlist).find("li").css({"background-color":"#fff"});
	      $(selectObject).css({"background-color":"#ddd"});
	   }
	   
	   return true;
	}
      }
      
   </script>
     
<style type="text/css">
  .txtright{
    text-align: right;
  }
  .authDiv{
    height:14px;width:120px;float:left;
  }
  
  .userlist{
    width:200px;
    height:auto;
    border:1px solid gray;
    background-color:white;
    position: absolute;
  }
  
  .userlist ul li{
     width:100%;
     height:20px;
     line-height:20px;
     border-bottom:1px dotted gray;
     font-size:12px;
  }
  
  .background1{
    background-color:#ececec;
  }
  
  .background2{
    background-color:#ffffff;
  }
</style>
